# -*- coding: utf-8 -*-
"""
Created on Thu Dec 31 05:37:33 2020

@author: zhujuxing
"""

import copy
import os
import time

import numpy as np
# import networkx as nx
import pandas as pd

import NetEvoConGen
import NetEvoObjMod
import NetEvoRulAna


def app_ava_cal(file, T, N, MT_time):
    """
    该函数为业务可用度计算主函数。

    Parameters
    ----------
    file : TYPE
        DESCRIPTION.
    T : TYPE
        DESCRIPTION.
    N : TYPE
        DESCRIPTION.

    Returns
    -------
    single_app_avail : TYPE
        DESCRIPTION.
    whole_app_avail : TYPE
        DESCRIPTION.

    """

    single_app_avail = pd.DataFrame(columns=[i + 1 for i in range(N)])
    whole_app_avail = 0.0
    g = NetEvoObjMod.CloudVritualizedNetwork(file, MT_time)

    for i in range(N):
        g_T = copy.deepcopy(g)  # 这里之前用的是copy.copy浅拷贝，导致后续修改g_T的时候使得g里的数据也发生了变动
        evol = NetEvoConGen.net_evo_con_gen(g_T, T)
        g_T = NetEvoRulAna.net_evo_rul_ana(g_T, evol)  # 修改net_evo_rul_ana_test为正式版函数名
        single_app_avail[i + 1] = g_T.graph['Application_info']['ApplicationDownTime'].apply(
            lambda x: 1 - (x / (T * 365 * 24)))
        # g_T.displayApp()
        # NetEvoRulAna.saveLog()
        del g_T
    NetEvoRulAna.clearVar()

    single_app_avail['result'] = single_app_avail.apply(np.mean, axis=1)
    print('单业务可用度计算结果为：' + os.linesep)
    print(single_app_avail)

    whole_app_avail = np.mean(single_app_avail['result'].to_list())
    print('整网业务可用度计算结果为：%f' % whole_app_avail)
    # g.displayApp(g)
    # return single_app_avail, whole_app_avail
    return single_app_avail, whole_app_avail


def test_T(MT_time):
    N = 20
    # file = os.path.abspath(os.path.dirname(os.getcwd())+os.path.sep+".")+os.sep+'test'+os.sep+'file.xlsx'
    file = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".") \
           + os.sep + '程序' + os.sep + "file.xls"
    result = pd.DataFrame()
    for T in [200]:
        t1 = time.time()
        single_app_avail, whole_app_avail = app_ava_cal(file, T, N, MT_time)
        single_app_avail.to_excel('result_(N100T%d).xlsx' % T)
        t2 = time.time()
        result_i = single_app_avail['result']
        result_i['whole'] = whole_app_avail
        result_i['time'] = t2 - t1
        result[T] = result_i
        result.to_excel('t_period.xlsx')
    # print("总用时：",round(t2-t1, 3),"秒")
    return single_app_avail, whole_app_avail


# def test_N():
#     t_period = 200
#     # file = os.path.abspath(os.path.dirname(os.getcwd())+os.path.sep+".")+os.sep+'test'+os.sep+'file.xlsx'
#     file = os.path.abspath(os.path.dirname(os.getcwd())+os.path.sep+".")\
#        +os.sep+'test'+os.sep+"file_128server - 副本.xlsx"
#     result = pd.DataFrame()
#     for N in [10,50,100,200,500]:
#         t1 = time.time()
#         single_app_avail, whole_app_avail = app_ava_cal(file, t_period, N)
#         single_app_avail.to_excel('128server_result_(T200N%d).xlsx'%N)
#         t2 = time.time()
#         result_i = single_app_avail['result']
#         result_i['whole'] = whole_app_avail
#         result_i['time'] = t2-t1
#         result[N] = result_i
#         result.to_excel("N.xlsx")
#     return result


if __name__ == '__main__':
    file = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".") \
           + os.sep + '程序' + os.sep + "file.xls"
    single_app_avail, whole_app_avail = app_ava_cal(file, T=500, N=100, MT_time=10)
    # result_N = test_N()
